package com.ygqh.baby.controller.mobile;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.PromotionType;
import com.ygqh.baby.model.BmdismModel;
import com.ygqh.baby.model.YgPromotionItemModel;
import com.ygqh.baby.model.YgPromotionModel;
import com.ygqh.baby.model.YgPromotionProductModel;
import com.ygqh.baby.redis.RedisDao;
import com.ygqh.baby.service.*;
import com.ygqh.baby.service.impl.DocumentConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 促销相关
 *
 * @author guohao
 */
@Controller
@RequestMapping("/mobile/promotion")
public class YgPromotionController extends BaseController {

    @Autowired
    private YgPromotionService promotionService;
    @Autowired
    private DocumentConstant documentConstant;
    @Autowired
    private YgProductPackService ygProductPackService;
    @Autowired
    private YgProductService ygProductService;
    @Autowired
    private RedisDao redisDao;
    @Autowired
    private YgBmdismService ygBmdismService;
    @Autowired
    private YgProductActionGiftService ygProductActionGiftService;

    /**
     * 获取促销商品列表
     *
     * @param pType
     * @param callback
     * @return
     */
    @RequestMapping(value = "findPromotionItemList", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject findPromotionItemList(PromotionType pType, BigDecimal orderPrice, String callback) {
        JSONPObject json = null;
        if (pType.equals(PromotionType.JiaJiaGou)) {
            orderPrice = null;
        }
        List<YgPromotionItemModel> promotionList = promotionService.findPromotionItemList(pType, orderPrice);
        json = new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, promotionList));
        return json;
    }

    /**
     * 获取
     *
     * @param callback
     * @return
     */
    @RequestMapping(value = "findOwnPromotionList", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject findOwnPromotionList(String callback) {
        JSONPObject json;
        Map<String, Object> map = ownPromotionListSegment();
        json = new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, map));
        return json;
    }

    /**
     * 查找加价购列表
     *
     * @param callback
     * @return
     */
    @RequestMapping(value = "findJiajiagoPromotionList", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject findJiajiagoPromotionList(String callback, Long productId) {
        JSONPObject json;
        List<YgPromotionModel> list = promotionService.findJiajiagoPromotionList(productId);
        json = new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, list));
        return json;
    }

    @RequestMapping(value = "findBmdismByProductIds", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject findBmdismByProductIds(String callback, Long... productIds) {
        List<BmdismModel> list = ygBmdismService.findByProductIds(productIds);
        return new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, list));
    }

    /**
     * 获取
     *
     * @param callback
     * @return
     */
    @RequestMapping(value = "findOwnPromotionListNotLogin", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject findOwnPromotionListNotLogin(String callback, Long productId, Long brandId) {

        Map<String, Object> map = promotionService.findOwnPromotionListNotLogin(productId, brandId);

        if (map.isEmpty()) {

            return new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, null));
        }
        return new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, map));
    }

    private Map<String, Object> ownPromotionListSegment() {
        List<YgPromotionModel> promotionList = promotionService.findOwnPromotionList(null);
//		List<YgProductPackModel> packList = ygProductPackService.findOwnProductPackList(null);
        Map<String, Object> map = new HashMap<String, Object>();
        if (promotionList != null && promotionList.size() > 0) {
            for (YgPromotionModel model : promotionList) {
                if (model.getPromotionType().equals(PromotionType.JiaJiaGou)) {
                    model.setPromotionTitle("满" + model.getLimitPrice() + "加" + model.getDiscountPrice() + "换购");
                    map.put("JiaJiaGou", model);
                }
                if (model.getPromotionType().equals(PromotionType.ManJian)) {
                    model.setPromotionTitle("全场满" + model.getLimitPrice() + "减" + model.getDiscountPrice());
                    map.put("ManJian", model);
                }
                // if (model.getPromotionType().equals(PromotionType.ManZeng)) {
                // map.put("ManZeng", model);
                // }
            }
        }
//		if (packList != null && packList.size() > 0) {
//			map.put("pack", packList);
//		}
        return map;
    }

    @RequestMapping(value = "getSingleGiftList", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject getSingleGift(String callback, Long... productIds) {
        List<YgPromotionProductModel> list = ygProductActionGiftService.getSingleGiftList(productIds);
        return new JSONPObject(callback, Message.success(documentConstant.MESSAGE_SUCCESS, list));
    }

}
